package views;

import bean.Cls;
import bean.Team;
import service.ClassService;
import utils.TableMode;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class TeamList extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTable table;
    Cls cls2=new Cls();
    public  void setCls(Cls cls) {
        cls2=cls;

    }
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TeamList frame = new TeamList();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TeamList() {
        setTitle("小组列表");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 731, 594);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(10, 10, 188, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("查询");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Team team=new Team();
                team.setC_id(cls2.getC_id());
                team.setT_name(textField.getText());


                //查询小组列表
                ClassService s=new ClassService();
                List<Team> aList=s.selectTeamByName(team);
                Vector data =new Vector();
                Iterator<Team> iterator=aList.iterator();
                while(iterator.hasNext()){
                    Team next = iterator.next();
                    Vector row=new Vector();
                    row.add(next.getT_id());
                    row.add(next.getT_name());
                    data.add(row);
                }

                Vector colNames=new Vector();
                colNames.add("小组ID");
                colNames.add("小组名称");
                TableMode mode=new TableMode(data, colNames);
                table.setModel(mode);
            }
        });
        btnNewButton.setBounds(208, 9, 93, 23);
        contentPane.add(btnNewButton);

        JButton btnNewButton_1_2 = new JButton("返回");
        btnNewButton_1_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Main m=new Main();
                m.setCls(cls2);
                m.setVisible(true);
                TeamList.this.dispose();
            }
        });
        btnNewButton_1_2.setBounds(311, 9, 93, 23);
        contentPane.add(btnNewButton_1_2);

        JButton btnNewButton_1_3 = new JButton("删除");
        btnNewButton_1_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 检查是否有行被选中
                int selectedRow = table.getSelectedRow();
                if (selectedRow == -1) {
                    // 若无行被选中，则弹出提示对话框
                    JOptionPane.showMessageDialog(null, "请先选择一行！", "提示", JOptionPane.WARNING_MESSAGE);
                } else {
                    Team team=new Team();
                    team.setT_id(table.getValueAt(table.getSelectedRow(), 0).toString());
                    ClassService service=new ClassService();
                    int n=service.deleteTeam(team);
                    if(n>=1) {
                        //lblNewLabel_5.setText("删除成功");
                        JOptionPane.showMessageDialog(null, "删除成功", "提示", JOptionPane.WARNING_MESSAGE);
                    }
                    else {
                        //lblNewLabel_5.setText("删除失败");
                        JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.WARNING_MESSAGE);
                    }
                }
            }
        });
        btnNewButton_1_3.setBounds(414, 9, 93, 23);
        contentPane.add(btnNewButton_1_3);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 41, 689, 495);
        contentPane.add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);

        //查询小组列表
        ClassService s=new ClassService();
        List<Team> aList=s.selectTeam();
        Vector data =new Vector();
        Iterator<Team> iterator=aList.iterator();
        while(iterator.hasNext()){
            Team next = iterator.next();
            Vector row=new Vector();
            row.add(next.getT_id());
            row.add(next.getT_name());
            data.add(row);
        }

        Vector colNames=new Vector();
        colNames.add("小组ID");
        colNames.add("小组名称");
        TableMode mode=new TableMode(data, colNames);
        table.setModel(mode);
    }


}
